package com.eam.asset.file.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eam.asset.file.domain.AssetOther;
import com.eam.asset.file.dto.AssetOther.AssetOtherAddDto;
import com.eam.asset.file.dto.AssetOther.AssetOtherDto;
import com.eam.asset.file.utils.AssetEncode;
import com.eam.common.constant.Constants;
import com.eam.common.core.redis.SRTemplateCache;
import com.eam.common.utils.DateUtils;
import com.eam.common.utils.SecurityUtils;
import com.eam.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.eam.asset.file.mapper.AssetOtherMapper;
import com.eam.asset.file.service.IAssetOtherService;

import static com.eam.asset.file.Enum.AssetEncodeEnum.ASSETOTHER;

/**
 * 其他资产档案(广告位等)Service业务层处理
 *
 * @author tzf
 * @date 2023-10-31
 */
@Service
public class AssetOtherServiceImpl extends ServiceImpl<AssetOtherMapper, AssetOther> implements IAssetOtherService {
    private final SRTemplateCache SRTemplateCache;

    @Autowired
    public AssetOtherServiceImpl(SRTemplateCache srTemplateCache) {
        SRTemplateCache = srTemplateCache;
    }

    /**
     * 查询其他资产档案(广告位等)
     *
     * @param otherId 其他资产档案(广告位等)主键
     * @return 其他资产档案(广告位等)
     */
    @Override
    public AssetOther selectAssetOtherByOtherId(String otherId) {
        //如果存在key,从redis中取值
        if (SRTemplateCache.hasKey(otherId)) {
            return SRTemplateCache.get(otherId, AssetOtherDto.class);
        }
        AssetOther assetOther = baseMapper.selectById(otherId);
        AssetOtherDto assetOtherDto = new AssetOtherDto();
        BeanUtils.copyProperties(assetOther, assetOtherDto);
        //存进redis
        SRTemplateCache.set(otherId, JSONUtil.toJsonStr(assetOtherDto));
        return assetOtherDto;
    }

    /**
     * 查询其他资产档案(广告位等)列表
     *
     * @param assetOther 其他资产档案(广告位等)
     * @return 其他资产档案(广告位等)
     */
    @Override
    public List<AssetOther> selectAssetOtherList(AssetOther assetOther) {
        //todo 后续做redis缓存,考虑数据一致性,如增删改操作需要考虑
        AssetOtherDto assetOtherDto = new AssetOtherDto();
        //深拷贝
        BeanUtils.copyProperties(assetOther, assetOtherDto);
        return baseMapper.selectAssetOtherList(assetOtherDto);
    }

    /**
     * 新增其他资产档案(广告位等)
     *
     * @param assetOther 其他资产档案(广告位等)
     * @return 结果
     */
    @Override
    public int insertAssetOther(AssetOther assetOther) {
        //创建dto对象,深拷贝属性值
        AssetOtherAddDto assetOtherAddDto = new AssetOtherAddDto();
        BeanUtils.copyProperties(assetOther, assetOtherAddDto);
        //设置基本的信息
        assetOtherAddDto.setCreateBy(AssetEncode.IfAbsent(SecurityUtils.getUserId()));
        assetOtherAddDto.setCreateTime(DateUtils.getNowDate());
        assetOtherAddDto.setStatus(Constants.ZERO);
        //设置资产档案编号,一级资产分类编码+二级资产分类编码+三级资产名称编码加编号
        StringBuffer encode = AssetEncode.getEncode(ASSETOTHER.getType()).
                append(AssetEncode.getEncode(assetOtherAddDto.getOtherCategory())).
                append(AssetEncode.getEncodeByName(assetOtherAddDto.getOtherName())).
                append(DateUtils.dateTime());
        assetOtherAddDto.setOtherCode(encode.toString());
        return baseMapper.insert(assetOtherAddDto);
    }

    /**
     * 修改其他资产档案(广告位等)
     *
     * @param assetOther 其他资产档案(广告位等)
     * @return 结果
     */
    @Override
    public int updateAssetOther(AssetOther assetOther) {
        assetOther.setUpdateTime(DateUtils.getNowDate());
        assetOther.setUpdateBy(AssetEncode.IfAbsent(SecurityUtils.getUserId()));
        AssetOtherDto assetOtherDto = new AssetOtherDto();
        BeanUtils.copyProperties(assetOther, assetOtherDto);
        return baseMapper.updateById(assetOtherDto);
    }

    /**
     * 批量删除其他资产档案(广告位等)
     *
     * @param otherIds 需要删除的其他资产档案(广告位等)主键
     * @return 结果
     */
    @Override
    public int deleteAssetOtherByOtherIds(String[] otherIds) {
        //批量删除根据ID
        return baseMapper.deleteBatchIds(Arrays.asList(otherIds));
    }

    /**
     * 删除其他资产档案(广告位等)信息
     *
     * @param otherId 其他资产档案(广告位等)主键
     * @return 结果
     */
    @Override
    public int deleteAssetOtherByOtherId(String otherId) {
        return baseMapper.deleteById(otherId);
    }
}
